<?
include 'classes/CommonClass.php';
include 'classes/GlobalConst.php';
global $smarty, $frm, $settings;
global $toprotatingbanners, $specialbanners, $normalbanners, $minibanners, $rotatingbanners;
$action = $frm ['action'];

if ($action == 'add_banner1') {
	$smarty->assign ( 'max_toprotating', $toprotatingbanners ['max_toprotating'] );
	$smarty->assign ( 'max_rotating', $rotatingbanners ['max_rotating'] );
	$smarty->assign ( 'max_special', $specialbanners ['max_special'] );
	$smarty->assign ( 'step_special', $specialbanners ['step_special'] );
	$smarty->assign ( 'max_normal', $normalbanners ['max_normal'] );
	$smarty->assign ( 'step_normal', $normalbanners ['step_normal'] );
	$smarty->assign ( 'max_mini', $minibanners ['max_mini'] );
	$smarty->assign ( 'step_mini', $minibanners ['step_mini'] );
	$smarty->assign ( 'expireddate_toprotating', $minibanners ['step_mini'] );
	$smarty->assign ( 'expireddate_rotating', $minibanners ['step_mini'] );
	$smarty->display ( 'add_banner.tpl' );
}else if($action == 'redirectLR'){
	
	//so tuan add vao quang cao. vd ad_time = 1, ad_time_type = m => add_week_number = 3
	$ad_week_number = 0;
	//fee quang cao/tuan ung voi moi loai quang cao
	$ad_fee_week = 0;
	//tong fee quang cao
	$ad_fee_total = 0;
	//vi tri cua quang cao
	$ad_position = 0;
	
	$period = "";
	
	
	
	$ad_type = $frm['ad_type'];
	$specialbannerposition  = $frm['specialbannerposition'];
	$normalbannerposition  = $frm['normalbannerposition'];
	$minibannerposition  = $frm['minibannerposition'];
	
	
	
	$ad_time  = quote($frm['ad_time']);
	$ad_time_type  = quote($frm['ad_time_type']); 
	$ad_website_url  = quote($frm['ad_website_url']);
	$contact_mail  = quote($frm['contact_mail']);
	$ad_banner_url  = quote($frm['ad_banner_url']);
	$description  = quote($frm['description']);
	$total_price1  = quote($frm['total_price1']);
	
	//check du lieu nhap vao tu server
//	if(!is_numeric($ad_type) || !is_numeric($ad_time) || !is_numeric($total_price)){
//		header ( "Location: ?a=error" );
//		exit();
//	}
	
	
	try {
		
		if($ad_time_type == GlobalConst::$AD_TIME_TYPE_WEEK){
			$ad_week_number = $ad_time;
			$period = $ad_time . " week";
			if($ad_time > 1){
				$period = $ad_time . " weeks";
			}
		}else if($ad_time_type == GlobalConst::$AD_TIME_TYPE_MONTH){
			$ad_week_number = $ad_time * 3;
			$period = $ad_time . " month";
			if($ad_time > 1){
				$period = $ad_time . " months";
			}
		}else{
			header ( "Location: ?a=error" );
			exit();
		}
		if(GlobalConst::$ADV_GROUP_TOP_ID == $ad_type){
			$ad_fee_week = $toprotatingbanners['max_toprotating'];
		}else if(GlobalConst::$ADV_GROUP_ROTATION_ID == $ad_type){
			$ad_fee_week = $rotatingbanners['max_rotating'];
		}else if(GlobalConst::$ADV_GROUP_SPECIAL_ID == $ad_type){
			if($specialbannerposition <= $specialbanners['num_special'] 
				&& !notexpired($specialbanners['expireddate_special'.$specialbannerposition])){
				$ad_fee_week = $specialbanners['max_special'] - ($specialbannerposition -1 ) * $specialbanners['step_special'];
				$ad_position = $specialbannerposition;
			}else{
				header ( "Location: ?a=error" );
			}
		}else if(GlobalConst::$ADV_GROUP_NORMAL_ID == $ad_type){
			if($normalbannerposition <= $normalbanners['num_normal'] 
				&& !notexpired($normalbanners['expireddate_normal'.$normalbannerposition])){
				$ad_fee_week = $normalbanners['max_normal'] - ($normalbannerposition -1 ) * $normalbanners['step_normal'];
				$ad_position = $normalbannerposition;
			}else{
				header ( "Location: ?a=error" );
				exit();
			}
		}else if(GlobalConst::$ADV_GROUP_MINI_ID == $ad_type){
			if($minibannerposition <= $minibanners['num_mini'] 
				&& !notexpired($minibanners['expireddate_mini'.$minibannerposition])){
				$ad_fee_week = $minibanners['max_mini'] - ($minibannerposition -1 ) * $minibanners['step_mini'];
				$ad_position = $minibannerposition;
			}else{
				header ( 'Location: ?a=error' );
				exit();
			}
		}else{
			header ( 'Location: ?a=error' );
			exit();
		}
		$ad_fee = $ad_fee_week * $ad_week_number;
		if($ad_fee != $total_price1){
			header ( 'Location: ?a=error' );
			exit();
		}
		$smarty->assign ( 'ad_type', $ad_type );
		$smarty->assign ( 'ad_position', $ad_position );
		$smarty->assign ( 'ad_week_number', $ad_week_number );
		$smarty->assign ( 'period', $period );
		$smarty->assign ( 'ad_website_url', $ad_website_url );
		$smarty->assign ( 'ad_banner_url', $ad_banner_url );
		$smarty->assign ( 'description', $description );
		$smarty->assign ( 'contact_mail', $contact_mail );
		$smarty->assign ( 'ad_fee', $ad_fee );
		
		$smarty->display ( 'add_banner_send_to_lr.tpl' );
		
		
	} catch (Exception $e) {
		header ( "Location: ?a=error" );
		exit();
	}
}else if($action == 'fromLR'){
	$say = $frm ['say'];
	if ('success' != $say) {
		$smarty->assign ( 'say', 'notsuccess' );
		$smarty->display ( 'lr_processing.tpl' );
		exit ();
	}
	$paidto = quote ( $frm ['lr_paidto'] );
	$paidby = quote ( $frm ['lr_paidby'] );
	$lr_amnt = quote ( $frm ['lr_amnt'] );
	$lr_currency = quote ( $frm ['lr_currency'] );
	$lr_transfer = quote ( $frm ['lr_transfer'] );
	$lr_store = quote ( $frm ['lr_store'] );
	$lr_encrypted = quote ( $frm ['lr_encrypted'] );
	$action_type = quote ( $frm ['action_type'] );
	$payment_type = quote ( $frm ['payment_type'] );
	
	$hash_recieved = $paidto . ':' . $paidby . ':' . stripslashes ( $lr_store ) . ':' . $lr_amnt . ':' . $lr_transfer . ':' . $lr_currency . ':' . $settings ['second_password'];
	
	$hash_encrypt = hash ( 'sha256', $hash_recieved );
	//print $hash_encrypt;
	//if de check dau vao dung, neu nguoi lai chuyen den trang error
	if (isset ( $paidto ) 
		&& $paidto == $settings ['admin_lr_account'] 
		&& isset ( $paidby ) 
		&& isset ( $lr_store ) 
		&& stripslashes ( $lr_store ) == $settings ['libertyreserve_store'] 
		&& isset ( $lr_amnt ) 
		&& isset ( $lr_transfer ) 
		&& isset ( $lr_currency ) 
		&& isset ( $lr_encrypted ) && $lr_encrypted == $hash_encrypt
		) {
		//fee quang cao/tuan ung voi moi loai quang cao
		$ad_fee_week = 0;
		//tong fee quang cao
		$ad_fee_total = 0;
		
		$ad_type = quote($frm['ad_type']);
		$ad_position = quote($frm['ad_position']);
		$ad_week_number = quote($frm['ad_week_number']);
		$ad_website_url = quote($frm['ad_website_url']);
		$ad_banner_url = quote($frm['ad_banner_url']);
		$description = quote($frm['description']);
		$contact_mail = quote($frm['contact_mail']);
		
		if(GlobalConst::$ADV_GROUP_TOP_ID == $ad_type){
			$ad_fee_week = $toprotatingbanners['max_toprotating'];
			$frm['ad_type'] = 'Top Rotating Banner';
		}else if(GlobalConst::$ADV_GROUP_ROTATION_ID == $ad_type){
			$ad_fee_week = $rotatingbanners['max_rotating'];
			$frm['ad_type'] = 'Rotating Banner';
		}else if(GlobalConst::$ADV_GROUP_SPECIAL_ID == $ad_type){
			if($ad_position <= $specialbanners['num_special'] 
				&& !notexpired($specialbanners['expireddate_special'.$ad_position])){
				$ad_fee_week = $specialbanners['max_special'] - ($ad_position -1 ) * $specialbanners['step_special'];
				$frm['ad_type'] = 'Special Banner';
			}else{
				header ( "Location: ?a=error" );
			}
		}else if(GlobalConst::$ADV_GROUP_NORMAL_ID == $ad_type){
			if($ad_position <= $normalbanners['num_normal'] 
				&& !notexpired($normalbanners['expireddate_normal'.$ad_position])){
				$ad_fee_week = $normalbanners['max_normal'] - ($ad_position -1 ) * $normalbanners['step_normal'];
				$frm['ad_type'] = 'Normal Banner';
			}else{
				header ( "Location: ?a=error" );
				exit();
			}
		}else if(GlobalConst::$ADV_GROUP_MINI_ID == $ad_type){
			if($ad_position <= $minibanners['num_mini'] 
				&& !notexpired($minibanners['expireddate_mini'.$ad_position])){
				$ad_fee_week = $minibanners['max_mini'] - ($ad_position -1 ) * $minibanners['step_mini'];
				$frm['ad_type'] = 'Mini Banner';
			}else{
				header ( 'Location: ?a=error' );
				exit();
			}
		}else{
			header ( 'Location: ?a=error' );
			exit();
		}
		$ad_fee_total = $ad_fee_week * $ad_week_number;	
		if($ad_fee_total < $lr_amnt){
			header ( "Location: ?a=error" );
			exit();
		}
		//ok cac dk da duoc thoa man roi, send mail cho admin de add quang cao
		send_mail ( 'advertisement_watting', $frm ['contact_mail'], $settings ['system_email'], $settings ['system_email_password'], $frm );
		send_mail ( 'advertisement_watting_notice_admin', $settings ['admin_email'], $settings ['system_email'], $settings ['system_email_password'], $frm );
		
		$smarty->assign ( 'say', 'success' );
		$smarty->assign ( 'action', 'add_adv' );
		$smarty->assign ( 'amount', $lr_amnt );
		$smarty->assign ( 'account', $paidby );
		$smarty->assign ( 'batch', $lr_transfer );
		$smarty->assign ( 'email', $frm ['contact_mail'] );
		
		$smarty->display ( 'lr_processing.tpl' );
		exit();
	}else{
		header ( "Location: ?a=error" );
		exit();
	}
}

?>
